package com.bwie.parkdemo.mvvm.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bwie.lib_core.mvvm.base.BaseActivity;
import com.bwie.parkdemo.BR;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.databinding.ActivityRepairBinding;
import com.bwie.parkdemo.mvvm.viewmodel.RepairViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 维修列表界面
 * */
public class RepairActivity extends BaseActivity<ActivityRepairBinding,RepairViewModel> implements View.OnClickListener {

    private TextView edtTv;
    private RelativeLayout bottom_relative;
    private ImageView repair_sel_all_img;

    @Override
    public int initLayout() {
        return R.layout.activity_repair;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        bottom_relative = findViewById(R.id.repair_bottom_relative);
        edtTv = findViewById(R.id.layout_title_add);
        repair_sel_all_img = findViewById(R.id.repair_sel_all_img);
        edtTv.setText("编辑");
        edtTv.setVisibility(View.VISIBLE);
        edtTv.setOnClickListener(this);
        repair_sel_all_img.setOnClickListener(this);
    }

    @Override
    public int initVariable() {
        return BR.vm;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.layout_title_add) {
            //根据文字做相关判断
            if (edtTv.getText().toString().equals("编辑")) {
                edtTv.setText("保存");
                //调用vm下对应方法->对应item显示可选择框
                //请求服务器接口获取维修人员列表
                vm.showSel();
                bottom_relative.setVisibility(View.VISIBLE);
            } else {
                edtTv.setText("编辑");
                bottom_relative.setVisibility(View.GONE);
                //调用vm下对应方法->调用服务器指定任务接口指定用户
                vm.appointRepair();
            }
        }else {
            //全部选中或全部不选
            boolean flag = vm.selAll();
            if(flag)
                repair_sel_all_img.setImageResource(R.drawable.gou);
            else
                repair_sel_all_img.setImageResource(R.drawable.gou_default);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshSelImg(String msg){
        if (vm.isFlag()){
            repair_sel_all_img.setImageResource(R.drawable.gou);
        }else {
            repair_sel_all_img.setImageResource(R.drawable.gou_default);
        }
    }
}
